import 'package:chathouse/model/species.dart';
import 'package:chathouse/public/Config.dart';
import 'package:chathouse/util/base_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get_core/src/get_main.dart';

import '../../model/expert.dart';
import '../../widgets/base_appbar.dart';
import '../base/base.dart';
import 'package:get/get.dart';

import 'buyWallet/pay_type.dart';
import 'buyWallet/wallet_cerList_page.dart';

class WealletPage extends BaseStatefulWidget {
  const WealletPage({Key? key}) : super(key: key);

  @override
  _WealletPageState createState() => _WealletPageState();
}

class _WealletPageState extends BaseStatefulWidgetState<WealletPage> {
  Species? species;
  Expert? info;

  @override
  void initState() {
    super.initState();
    updateEmptyPage(tipTitle: "");
    updateState(LoadState.success);
    setState(() {
      scaffoldBackColor = Colors.white;
      showAppBar = false;
    });

    loadSpeciesList();
    loadMemberInfo();
  }

  void loadSpeciesList() {
    Obj.showLoading();
    Apis.speciesList()
        // ignore: avoid_print
        .then((response) => {
              Obj.dismissLoading(),
              setState(() {
                var data = response?.data;
                species = Species.fromJson(data);
                setState(() {});
              })
            })
        .onError((error, stackTrace) => {Obj.dismissLoading(), print("aa")});
  }

  void loadMemberInfo() {
    Apis.expertInfo()
        .then((response) => {
              setState(() {
                var data = response?.data["data"];
                info = Expert.fromJson(data);
                setState(() {});
              })
            })
        .onError((error, stackTrace) => {});
  }

  Widget itemCell(Rows? item) {
    return Container(
      decoration: BoxDecoration(
          color: Styles.mainColor, borderRadius: BorderRadius.circular(30)),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            "${item?.tokenAmount ?? ""}個",
            style: getTextStyle(24.sp,
                fontType: FontType.BOLD, color: Colors.white),
            textAlign: TextAlign.center,
          ),
          Text(
            "HK\$${item?.moneyAmount ?? ""}",
            style: getTextStyle(26.sp,
                fontType: FontType.BOLD, color: Colors.white),
            textAlign: TextAlign.center,
          ),
        ],
      ),
    ).setOnClickListener(() {
      // Get.to(PayType());
      Get.to(PayType(item));
    });
  }

  void cerList() {
    Get.to(const WalletCerListPage());
  }

  @override
  Widget viewPageBody() {
    return Scaffold(
        appBar: AppBar(
          leading: Icon(
            Icons.arrow_back_ios,
            size: 18.w,
            color: MyColors.gary_149149149,
          ).setOnClickListener(() {
            Get.back();
          }),
          title: Text(
            "管理金幣銀包",
            textAlign: TextAlign.center,
            style: getTextStyle(28.sp,
                color: Colors.black, fontType: FontType.MID),
          ),
          actions: [
            Container(
              padding: const EdgeInsets.only(right: 12),
              child: Center(
                child: Text(
                  "充值記錄",
                  textAlign: TextAlign.center,
                  style: getTextStyle(20.sp,
                      color: Styles.mainColor, fontType: FontType.BOLD),
                ),
              ),
            ).setOnClickListener(() {
              cerList();
            })
          ],
        ),
        body: ListView(
            padding: EdgeInsets.symmetric(horizontal: 24.w),
            children: [
              Text(
                "金幣餘額:${info?.balance}個",
                style: getTextStyle(30.sp,
                    fontType: FontType.BOLD, color: Styles.mainColor),
                textAlign: TextAlign.center,
              ).setContainer(margin: EdgeInsets.only(top: Styles.magin_8)),
              Text(
                '購買金幣',
                style: getTextStyle(30.sp,
                    fontType: FontType.BOLD, color: Styles.c_333),
                textAlign: TextAlign.center,
              ).setContainer(margin: EdgeInsets.only(top: Styles.magin_12)),
              SizedBox(
                height: hc_ScreenHeight() - 88 - 44 - 200,
                child: GridView.builder(
                  padding: EdgeInsets.symmetric(vertical: 24.w),
                  shrinkWrap: true,
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    //设置列数
                    crossAxisCount: 2,
                    //设置横向间距
                    crossAxisSpacing: 20,
                    //设置主轴间距
                    mainAxisSpacing: 8,
                    childAspectRatio: 1.6,
                  ),
                  scrollDirection: Axis.vertical,
                  itemCount: species?.rows?.length ?? 0,
                  itemBuilder: (context, index) {
                    return itemCell(species?.rows?[index]);
                  },
                ),
              ),
              // Text(
              //   '無限接Job月費優惠 \n 數量有限,詳情查詢 \n info@chathouse.com.hk',
              //   style: getTextStyle(22.sp,
              //       fontType: FontType.BOLD, color: Styles.mainColor),
              //   textAlign: TextAlign.center,
              // ).setContainer(margin: EdgeInsets.only(top: Styles.magin_12))
            ]));
  }
}
